﻿using System.Collections.Generic;
using System.Text;

namespace DimsMapper.Modules
{
    static class LstFiles
    {
        static readonly string[] protoLstPath = {
            "proto\\items\\items.lst",
            "proto\\critters\\critters.lst",
            "proto\\scenery\\scenery.lst",
            "proto\\walls\\walls.lst",
            "proto\\tiles\\tiles.lst",
            "proto\\misc\\misc.lst"
        };

        static readonly string[] frmLstPath = {
            "art\\items\\items.lst",
            "art\\critters\\critters.lst",
            "art\\scenery\\scenery.lst",
            "art\\walls\\walls.lst",
            "art\\tiles\\tiles.lst",
            "art\\misc\\misc.lst",
            "art\\intrface\\intrface.lst",
            "art\\inven\\inven.lst",
        };

        // имена файлов
        static List<string>[] lstPRO;
        static List<string>[] lstFRM;
        static List<string> lstScripts;

        static int initResult = 0; // -1 при ошибке

        public static bool IsInitilization()
        {
            return initResult == 1;
        }

        public static int InitilizationLst()
        {
            if (initResult == 1) return initResult;

            if (initResult == 0) {
                lstPRO = new List<string>[6];
                lstFRM = new List<string>[8];
            }

            for (int i = 0; i < 6; i++)
            {
                lstPRO[i] = DatFiles.ReadFile(protoLstPath[i], Encoding.ASCII);
                if (lstPRO[i] == null) {
                    initResult = -1;
                    return initResult;
                }
            }

            for (int i = 0; i < 8; i++)
            {
                lstFRM[i] = DatFiles.ReadFile(frmLstPath[i], Encoding.ASCII);
                if (lstFRM[i] == null) {
                    initResult = -1;
                    return initResult;
                }
            }

            lstScripts = DatFiles.ReadFile("scripts\\scripts.lst", Settings.encoding);
            if (lstScripts == null) {
                initResult = -1;
            } else {
                initResult = 1;
            }

            ProtoFiles.Initialization();
            FrmFiles.Initialization();
            MsgFiles.LoadMessages();

            return initResult;
        }

        public static void ClearAll()
        {
            for (int i = 0; i < 6; i++)
            {
                lstPRO[i].Clear();
            }
            for (int i = 0; i < 8; i++)
            {
                lstFRM[i].Clear();
            }
            lstScripts.Clear();
            initResult = 2;
        }

        public static string ProtoNameFile(int type, int index)
        {
            return lstPRO[type][index - 1];
        }

        public static string FrmNameFile(int type, int index)
        {
            return (index < lstFRM[type].Count) ? lstFRM[type][index] : null;
        }

        public static string ScriptNameFile(int type, int index)
        {
            return lstScripts[index - 1];
        }

        public static int ProTotalCount(Object.Types type)
        {
            if (type == Object.Types.Inventory) type = Object.Types.Item;
            return lstPRO[(int)type].Count;
        }

        public static int FrmTotalCount(Object.Types type)
        {
            return lstFRM[(int)type].Count;
        }
    }
}
